import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";

import http from '../../http/index'
import { GroupInfo } from "../../domain/group";

const initialState: { value: {} } = {
  value: {},
};



export const fetchGroupInfo: any = createAsyncThunk('/group/info', async (id: any) => {
  const mapStr = localStorage.getItem("group_map");
  const groupMap = mapStr ? JSON.parse(mapStr) : {};
  const response = await http.get("/group/info?id="+id); 
  if(response.data){
    const groupInfo = response.data;
    groupMap[`${groupInfo.groupId}`] = groupInfo;
  }
  return groupMap;
})


export const groupSlice = createSlice({
  name: "group",
  initialState,
  reducers: {

  },
  extraReducers: (builder) => {
    builder.addCase(fetchGroupInfo.fulfilled, (state, action) => {
      const payload: any = action.payload;
      state.value = payload;
      localStorage.setItem("group_map", JSON.stringify(payload));
    })
  },
})

export const { } = groupSlice.actions;

export default groupSlice.reducer